#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.8
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# define the imbalanced covaraites
imbRep <- c("race2", "edu2", "pid2", "region2")


# Fitting -----------------------------------------------------------------

s8_1 <- noCov(immPCA, sample = "Replication", chatty = F)
s8_2 <- allCov(immPCA, sample = "Replication", chatty = F)
s8_3 <- flexCov(immPCA, sample = "Replication", covs = imbRep, chatty = F)

s8_4 <- noCov(imm_neighbors2, sample = "Replication", chatty = F)
s8_5 <- allCov(imm_neighbors2, sample = "Replication", chatty = F)
s8_6 <- flexCov(imm_neighbors2, sample = "Replication", covs = imbRep, chatty = F)

s8_7 <- noCov(imm_increase2, sample = "Replication", chatty = F)
s8_8 <- allCov(imm_increase2, sample = "Replication", chatty = F)
s8_9 <- flexCov(imm_increase2, sample = "Replication", covs = imbRep, chatty = F)


# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

texregPS(
  list(s8_1, s8_2, s8_3),
  custom.model.names = paste0("(", 1:3, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s8_4, s8_5, s8_6),
  custom.model.names = paste0("(", 4:6, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)

texregPS(
  list(s8_7, s8_8, s8_9),
  custom.model.names = paste0("(", 7:9, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "$\\checkmark$", ""),
    "Imbalanced Covaraites" = c("", "", "$\\checkmark$")
  )
)
